﻿using ComponentFactory.Krypton.Toolkit;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StableDiffusionTools.Controls.KryptonHelper
{
    public class KryptonFormPalettleHelper : Component, ISupportInitialize
    {
        public void BeginInit() { }

        public void EndInit()
        {
            ContainerControl.BackColor = _palette?.GetBackColor1(PaletteBackStyle.ControlClient, ContainerControl.Enabled ? PaletteState.Normal : PaletteState.Disabled) ?? ContainerControl.BackColor;
        }

        public Form ContainerControl { get; set; } = null;

        public override ISite Site
        {
            get { return base.Site; }
            set
            {
                base.Site = value;
                if (value == null)
                {
                    return;
                }

                IDesignerHost host = value.GetService(typeof(IDesignerHost)) as IDesignerHost;
                if (host != null)
                {
                    IComponent componentHost = host.RootComponent;
                    if (componentHost is Form)
                    {
                        ContainerControl = componentHost as Form;
                    }
                }
            }
        }

        private IPalette _palette;

        public KryptonFormPalettleHelper(IContainer container) : base()
        {
            _palette = KryptonManager.CurrentGlobalPalette;
            if (_palette != null)
                _palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(OnPalettePaint);

            KryptonManager.GlobalPaletteChanged += new EventHandler(OnGlobalPaletteChanged);
            container.Add(this);
        }

        private void OnPalettePaint(object sender, PaletteLayoutEventArgs e)
        {
            //ContainerControl.Invalidate();
        }

        private void OnGlobalPaletteChanged(object sender, EventArgs e)
        {
            if (_palette != null)
                _palette.PalettePaint -= new EventHandler<PaletteLayoutEventArgs>(OnPalettePaint);

            _palette = KryptonManager.CurrentGlobalPalette;

            if(ContainerControl != null)
                ContainerControl.BackColor = _palette?.GetBackColor1(PaletteBackStyle.ControlClient, ContainerControl.Enabled ? PaletteState.Normal : PaletteState.Disabled) ?? ContainerControl.BackColor;

            if (_palette != null)
                _palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(OnPalettePaint);

            //ContainerControl?.Invalidate();
        }

        #region IDisposable Support
        private bool disposedValue = false; 

        protected override void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    if (_palette != null)
                    {
                        _palette.PalettePaint -= new System.EventHandler<ComponentFactory.Krypton.Toolkit.PaletteLayoutEventArgs>(OnPalettePaint);
                        _palette = null;
                    }

                    ContainerControl = null;
                    ComponentFactory.Krypton.Toolkit.KryptonManager.GlobalPaletteChanged -= new System.EventHandler(OnGlobalPaletteChanged);
                }
                disposedValue = true;
            }
            base.Dispose(disposing);
        }
        #endregion
    }
}
